﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Threading;
using System.ServiceModel;
using System.IO;
using LINQAmazonProvider;  
using Common;
//using SerchEngine;



namespace LINQAmazonProvider
{
    public class AmazonSearch
    {
        private static string ListResult;//List<Common.DataTypes.Item> ListResult; //

        public static ManualResetEvent alldone;

        

        // Performs the REST search
        public string PerformWebServiceSearch(Dictionary<string, string> parameters)
        {
            alldone = new ManualResetEvent(false); 
            //ListResult = default(T);
            string baseURL = "http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&Operation=ItemSearch&AWSAccessKeyId=0TD9TEYEE3NDY01M6D02&SearchIndex=Books&ResponseGroup=Medium";
            foreach (KeyValuePair<string, string> kvp in parameters)
            {
                if (kvp.Value != "" && kvp.Value != "0")
                {
                    baseURL += "&" + kvp.Key + "=" + kvp.Value;
                }
            }
            RestDataAccess da = new RestDataAccess(baseURL);
            da.RestRequestCompleted += new RestDataAccess.RestRequestCompletedHandler(da_RestRequestCompleted);
            da.PerformRestRequest();            
            alldone.WaitOne();
            alldone.Close();
                                   
            return ListResult;
        }

        static void da_RestRequestCompleted(object sender, RestRequestCompletedEventArgs e)
        {
            //Posible proceso del response
            ListResult = e.Result; //(new ResultXMLParser(e.Result)).Parse(); ////
            alldone.Set();
        }
    }
}
